import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { WeChatFeature, UserInfo, PaymentInfo, AuthInfo, ShareInfo, LocationInfo } from '@/types'

export const useWeChatStore = defineStore('wechat', () => {
  // 状态
  const userInfo = ref<UserInfo | null>(null)
  const authInfo = ref<AuthInfo | null>(null)
  const paymentHistory = ref<PaymentInfo[]>([])
  const shareHistory = ref<ShareInfo[]>([])
  const locationInfo = ref<LocationInfo | null>(null)
  const isLoading = ref(false)

  // 微信功能列表
  const features = ref<WeChatFeature[]>([
    {
      id: 'payment',
      title: '微信支付',
      description: '集成微信支付功能，支持扫码支付、H5支付等',
      icon: '💰',
      route: '/payment',
      color: '#07c160',
      status: 'active'
    },
    {
      id: 'login',
      title: '一键登录',
      description: '微信一键登录，快速获取用户信息',
      icon: '🔐',
      route: '/login',
      color: '#576b95',
      status: 'active'
    },
    {
      id: 'auth',
      title: '授权登录',
      description: '微信授权登录，获取用户详细信息',
      icon: '🔑',
      route: '/auth',
      color: '#ff976a',
      status: 'active'
    },
    {
      id: 'share',
      title: '微信分享',
      description: '分享内容到微信好友和朋友圈',
      icon: '📤',
      route: '/share',
      color: '#fa5151',
      status: 'active'
    },
    {
      id: 'qrcode',
      title: '二维码',
      description: '生成和识别微信二维码',
      icon: '📱',
      route: '/qrcode',
      color: '#9c27b0',
      status: 'active'
    },
    {
      id: 'location',
      title: '位置服务',
      description: '获取用户地理位置信息',
      icon: '📍',
      route: '/location',
      color: '#607d8b',
      status: 'active'
    }
  ])

  // 计算属性
  const isLoggedIn = computed(() => !!userInfo.value)
  const activeFeatures = computed(() => features.value.filter(f => f.status === 'active'))

  // 动作
  const setUserInfo = (info: UserInfo) => {
    userInfo.value = info
  }

  const setAuthInfo = (info: AuthInfo) => {
    authInfo.value = info
  }

  const addPayment = (payment: PaymentInfo) => {
    paymentHistory.value.unshift(payment)
  }

  const addShare = (share: ShareInfo) => {
    shareHistory.value.unshift(share)
  }

  const setLocation = (location: LocationInfo) => {
    locationInfo.value = location
  }

  const setLoading = (loading: boolean) => {
    isLoading.value = loading
  }

  const clearUserData = () => {
    userInfo.value = null
    authInfo.value = null
    paymentHistory.value = []
    shareHistory.value = []
    locationInfo.value = null
  }

  return {
    // 状态
    userInfo,
    authInfo,
    paymentHistory,
    shareHistory,
    locationInfo,
    isLoading,
    features,
    // 计算属性
    isLoggedIn,
    activeFeatures,
    // 动作
    setUserInfo,
    setAuthInfo,
    addPayment,
    addShare,
    setLocation,
    setLoading,
    clearUserData
  }
})

